package com.example.newsapp.ui.components.newsComponent

import android.util.Log
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.compositionLocal.LocalNavControllerTab
import com.example.newsapp.data.list.tabList
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.data.`object`.TabNavigation
import com.example.newsapp.ui.components.mainComponent.BottomNavtionSelected

/**
 * 顶部标签栏
 */
@Composable
fun TopTabList(
    modifier: Modifier = Modifier
) {
    val navControllerTab = LocalNavControllerTab.current
    // 选择状态
    var selected by rememberSaveable { mutableStateOf(0) }
    ScrollableTabRow(
        selectedTabIndex = selected,
        edgePadding = 0.dp,
    ) {
        tabList.forEachIndexed { index, title ->
            Tab(
                selected = index == selected,
                onClick = {
                    StateSave.initState++
                    StateSave.previousTab = selected
                    selected = index
                    StateSave.currentTab = selected
                    StateSave.tabNum = selected
                    // 选择相同的标签刷新页面
                    if (StateSave.previousTab == StateSave.currentTab) {
                        StateSave.isRefresh = true
                        StateSave.keyRefresh++
                    } else {
                        StateSave.isNewsDetail = false
                    }
                    BottomNavtionSelected(
                        navController = navControllerTab,
                        route = when (StateSave.tabNum) {
                            0 -> TabNavigation.suggestTab
                            1 -> TabNavigation.hotTab
                            2 -> TabNavigation.headTab
                            3 -> TabNavigation.NBATab
                            4 -> TabNavigation.carTab
                            5 -> TabNavigation.jokeTab
                            6 -> TabNavigation.internationTab
                            else -> ""
                        },
                    )
                },
                text = {
                    Text(
                        text = title,
                        style = MaterialTheme.typography.titleMedium
                    )
                },
                modifier = Modifier.height(40.dp),
                selectedContentColor = Color.Black,
                unselectedContentColor = Color(0xFF565656)
            )
        }
    }
}